package net.rrkk.photoman.controller;

import net.rrkk.photoman.entity.PhotoCategory;
import net.rrkk.photoman.service.PhotoCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/categories")
public class PhotoCategoryController {
    
    @Autowired
    private PhotoCategoryService categoryService;
    
    // 创建分类
    @PostMapping
    public ResponseEntity<PhotoCategory> createCategory(@RequestBody PhotoCategory category) {
        PhotoCategory createdCategory = categoryService.createCategory(category);
        return new ResponseEntity<>(createdCategory, HttpStatus.CREATED);
    }
    
    // 获取所有分类
    @GetMapping
    public ResponseEntity<List<PhotoCategory>> getAllCategories() {
        List<PhotoCategory> categories = categoryService.getAllCategories();
        return new ResponseEntity<>(categories, HttpStatus.OK);
    }
    
    // 根据ID获取分类
    @GetMapping("/{id}")
    public ResponseEntity<PhotoCategory> getCategoryById(@PathVariable Long id) {
        Optional<PhotoCategory> category = categoryService.getCategoryById(id);
        if (category.isPresent()) {
            return new ResponseEntity<>(category.get(), HttpStatus.OK);
        } else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }
    
    // 获取顶级分类
    @GetMapping("/top-level")
    public ResponseEntity<List<PhotoCategory>> getTopLevelCategories() {
        List<PhotoCategory> categories = categoryService.getTopLevelCategories();
        return new ResponseEntity<>(categories, HttpStatus.OK);
    }
    
    // 获取启用的分类
    @GetMapping("/active")
    public ResponseEntity<List<PhotoCategory>> getActiveCategories() {
        List<PhotoCategory> categories = categoryService.getActiveCategories();
        return new ResponseEntity<>(categories, HttpStatus.OK);
    }
    
    // 根据父ID获取子分类
    @GetMapping("/sub/{parentId}")
    public ResponseEntity<List<PhotoCategory>> getSubCategories(@PathVariable Long parentId) {
        List<PhotoCategory> categories = categoryService.getSubCategories(parentId);
        return new ResponseEntity<>(categories, HttpStatus.OK);
    }
    
    // 根据名称搜索分类
    @GetMapping("/search")
    public ResponseEntity<List<PhotoCategory>> searchCategories(@RequestParam String name) {
        List<PhotoCategory> categories = categoryService.searchCategoriesByName(name);
        return new ResponseEntity<>(categories, HttpStatus.OK);
    }
    
    // 更新分类
    @PutMapping("/{id}")
    public ResponseEntity<PhotoCategory> updateCategory(@PathVariable Long id, @RequestBody PhotoCategory categoryDetails) {
        PhotoCategory updatedCategory = categoryService.updateCategory(id, categoryDetails);
        if (updatedCategory != null) {
            return new ResponseEntity<>(updatedCategory, HttpStatus.OK);
        } else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }
    
    // 删除分类
    @DeleteMapping("/{id}")
    public ResponseEntity<HttpStatus> deleteCategory(@PathVariable Long id) {
        categoryService.deleteCategory(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
    
    // 获取分类树结构
    @GetMapping("/tree")
    public ResponseEntity<List<Object[]>> getCategoryTree() {
        List<Object[]> categoryTree = categoryService.getCategoryTree();
        return new ResponseEntity<>(categoryTree, HttpStatus.OK);
    }
}